package com.mxnet.chongjin.nci.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.SpannableString;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mxnet.chongjin.nci.R;
import com.mxnet.chongjin.nci.base.BaseActivity;
import com.mxnet.chongjin.nci.bean.Report;
import com.mxnet.chongjin.nci.config.Api;
import com.mxnet.chongjin.nci.view.ItemReportView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class ReportActivity extends BaseActivity {
    private Context mContext;
    private Button mButtonQuit, mButtonSlCheck, mButtonSxCheck, mButtonError;
    private TextView mTextViewLot;
    private TextView mTextViewQa;
    private TextView mTextViewWg;
    private LinearLayout mLayoutSl, mLayoutSx;
    private SharedPreferences mPreferences;
    private List<Report> mSlReportList, mSxReportList;
    private List<ItemReportView> mSlItemViewList, mSxItemViewList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_report);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); // 强制横屏
        mContext = ReportActivity.this;
        mPreferences = getPreferences();

        SpannableString lot = (SpannableString) getIntent().getExtras().get("lot");

        if (lot.toString() != null)
            Log.i("*****onCreate", lot.toString());

        mSlReportList = new ArrayList<>();
        mSxReportList = new ArrayList<>();
        mSlItemViewList = new ArrayList<>();
        mSxItemViewList = new ArrayList<>();

        mTextViewLot = findViewById(R.id.textview_report_lot);
        mTextViewLot.setText(lot.toString());

        mTextViewQa = findViewById(R.id.textview_report_qa);
        mTextViewWg = findViewById(R.id.textview_report_wg);

        mLayoutSl = findViewById(R.id.ll_report_sl);

        mLayoutSx = findViewById(R.id.ll_report_sx);

        initData();

        mButtonError = findViewById(R.id.btn_report_error);
        mButtonError.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(mTextViewLot.getText())) {
                    Toast.makeText(mContext, "批号为空！", Toast.LENGTH_SHORT).show();
                } else {
                    String checkType = "";
                    if (mButtonError.getText().equals("正常")) {
                        checkType = "error";
                    } else {
                        checkType = "normal";
                    }
                    final String ct = checkType;
                    OkHttpUtils.get()
                            .url(mPreferences.getString("URL_HEADER", "") + Api.CHECK_STOCK)
                            .addParams("checkType", checkType)
                            .addParams("lot", mTextViewLot.getText().toString())
                            .build()
                            .execute(new StringCallback() {
                                @Override
                                public void onError(Call call, Exception e, int id) {
                                    Log.i("*****==onError==", e.getMessage());
                                }

                                @Override
                                public void onResponse(String response, int id) {
                                    if (!TextUtils.isEmpty(response)) {
                                        try {
                                            JSONObject jsonObject = new JSONObject(response);
                                            boolean isSuccess = jsonObject.optBoolean("success");
                                            if (isSuccess) {
                                                Toast.makeText(mContext, jsonObject.optString("message"), Toast.LENGTH_SHORT).show();
                                                if (ct.equals("error")) {
                                                    mButtonError.setBackground(getResources().getDrawable(R.drawable.table_style));
                                                    mButtonError.setText("异常");
                                                } else {
                                                    mButtonError.setBackground(getResources().getDrawable(R.drawable.btn_style));
                                                    mButtonError.setText("正常");
                                                }
                                            }
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }
                            });
                }

            }
        });

        mButtonSlCheck = findViewById(R.id.btn_report_sl_check);
        mButtonSlCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(mTextViewLot.getText())) {
                    Toast.makeText(mContext, "批号为空！", Toast.LENGTH_SHORT).show();
                } else {
                    String checkType = "";
                    if (mButtonSlCheck.getText().equals("盖章")) {
                        checkType = "sl";
                    } else {
                        checkType = "nosl";
                    }
                    final String ct = checkType;
                    OkHttpUtils.get()
                            .url(mPreferences.getString("URL_HEADER", "") + Api.CHECK_STOCK)
                            .addParams("checkType", checkType)
                            .addParams("lot", mTextViewLot.getText().toString())
                            .build()
                            .execute(new StringCallback() {
                                @Override
                                public void onError(Call call, Exception e, int id) {
                                    Log.i("*****==onError==", e.getMessage());
                                }

                                @Override
                                public void onResponse(String response, int id) {
                                    if (!TextUtils.isEmpty(response)) {
                                        try {
                                            JSONObject jsonObject = new JSONObject(response);
                                            boolean isSuccess = jsonObject.optBoolean("success");
                                            if (isSuccess) {
                                                Toast.makeText(mContext, jsonObject.optString("message"), Toast.LENGTH_SHORT).show();
                                                if (ct.equals("sl")) {
                                                    mButtonSlCheck.setBackground(getResources().getDrawable(R.drawable.table_style));
                                                    mButtonSlCheck.setText("已盖章");
                                                } else {
                                                    mButtonSlCheck.setBackground(getResources().getDrawable(R.drawable.btn_style));
                                                    mButtonSlCheck.setText("盖章");
                                                }
                                            }
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }
                            });
                }
            }
        });

        mButtonSxCheck = findViewById(R.id.btn_report_sx_check);
        mButtonSxCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(mTextViewLot.getText())) {
                    Toast.makeText(mContext, "批号为空！", Toast.LENGTH_SHORT).show();
                } else {
                    String checkType = "sx";
                    if (mButtonSxCheck.getText().equals("已盖章")) {
                        checkType = "nosx";
                    }
                    final String ct = checkType;
                    OkHttpUtils.get()
                            .url(mPreferences.getString("URL_HEADER", "") + Api.CHECK_STOCK)
                            .addParams("checkType", checkType)
                            .addParams("lot", mTextViewLot.getText().toString())
                            .build()
                            .execute(new StringCallback() {
                                @Override
                                public void onError(Call call, Exception e, int id) {
                                    Log.i("*****==onError==", e.getMessage());
                                }

                                @Override
                                public void onResponse(String response, int id) {
                                    if (!TextUtils.isEmpty(response)) {
                                        try {
                                            JSONObject jsonObject = new JSONObject(response);
                                            boolean isSuccess = jsonObject.optBoolean("success");
                                            if (isSuccess) {
                                                Toast.makeText(mContext, jsonObject.optString("message"), Toast.LENGTH_SHORT).show();
                                                if (ct.equals("sx")) {
                                                    mButtonSxCheck.setBackground(getResources().getDrawable(R.drawable.table_style));
                                                    mButtonSxCheck.setText("已盖章");
                                                } else {
                                                    mButtonSxCheck.setBackground(getResources().getDrawable(R.drawable.btn_style));
                                                    mButtonSxCheck.setText("盖章");
                                                }
                                            }
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }
                            });
                }
            }
        });

        mButtonQuit = findViewById(R.id.btn_report_quit);
        mButtonQuit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public void initData() {
        if (TextUtils.isEmpty(mTextViewLot.getText())) {
            Toast.makeText(mContext, "批号为空！", Toast.LENGTH_SHORT).show();
        } else {
            OkHttpUtils.get()
                    .url(mPreferences.getString("URL_HEADER", "") + Api.LOT_REPORT)
                    .addParams("lot", mTextViewLot.getText().toString())
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            Log.i("*****==onError==", e.getMessage());
                        }

                        @Override
                        public void onResponse(String response, int id) {
                            if (!TextUtils.isEmpty(response)) {
                                Log.i("*****==onReponse1==", response);
                                try {
                                    JSONObject jsonObject = new JSONObject(response);
                                    boolean isSuccess = jsonObject.optBoolean("success");
                                    if (isSuccess) {
                                        JSONObject data = jsonObject.optJSONObject("data");
                                        String qa = data.getString("qa");
                                        mTextViewQa.setText(qa);
                                        String wg = data.getString("wg");
                                        mTextViewWg.setText(wg);

                                        boolean isError = data.optBoolean("is_error");
                                        if (isError) {
                                            mButtonError.setBackground(getResources().getDrawable(R.drawable.table_style));
                                            mButtonError.setText("异常");
                                        } else {
                                            mButtonError.setBackground(getResources().getDrawable(R.drawable.btn_style));
                                            mButtonError.setText("正常");
                                        }

                                        boolean slChecked = data.optBoolean("sl_checked");
                                        // 数量盖章
                                        if (!slChecked) {
                                            mButtonSlCheck.setBackground(getResources().getDrawable(R.drawable.btn_style));
                                            mButtonSlCheck.setText("盖章");
                                            //mButtonSlCheck.setClickable(true);
                                        } else {
                                            mButtonSlCheck.setBackground(getResources().getDrawable(R.drawable.table_style));
                                            mButtonSlCheck.setText("已盖章");
                                            //mButtonSlCheck.setClickable(false);
                                        }
                                        // 色相盖章
                                        boolean sxChecked = data.optBoolean("sx_checked");
                                        if (!sxChecked) {
                                            mButtonSxCheck.setBackground(getResources().getDrawable(R.drawable.btn_style));
                                            mButtonSxCheck.setText("盖章");
                                            //mButtonSxCheck.setClickable(true);
                                        } else {
                                            mButtonSxCheck.setBackground(getResources().getDrawable(R.drawable.table_style));
                                            mButtonSxCheck.setText("已盖章");
                                            //mButtonSxCheck.setClickable(false);
                                        }

                                        // 数量数据初始化
                                        JSONArray slArray = data.optJSONArray("sl");
                                        for (int i = 0; i < slArray.length(); i++) {
                                            JSONObject slObject = slArray.optJSONObject(i);
                                            Report slReport = new Report();
                                            slReport.setLot(slObject.optString("lot"));
                                            slReport.setLength(slObject.optString("length_width"));
                                            slReport.setProperty(slObject.optString("s&m"));
                                            slReport.setArch(slObject.optString("jno"));
                                            slReport.setCheckqty(slObject.optInt("checkqty"));
                                            slReport.setInqty(slObject.optInt("inqty"));
                                            mSlReportList.add(slReport);
                                            ItemReportView slItemReportView = new ItemReportView(mContext, R.layout.item_report);
                                            slItemReportView.getTextView2().setText(slObject.optString("length_width"));
                                            slItemReportView.getTextView3().setText(slObject.optString("s&m"));
                                            slItemReportView.getTextView4().setText(slObject.optString("jno"));

                                            slItemReportView.getTextView4().setOnClickListener(new View.OnClickListener() {
                                                @Override
                                                public void onClick(View v) {
                                                    AlertDialog alertDialog1 = new AlertDialog.Builder(mContext)
                                                            .setTitle("券号")//标题
                                                            .setMessage(((TextView) v).getText())//内容
                                                            .create();
                                                    alertDialog1.show();

                                                }
                                            });

                                            slItemReportView.getTextView5().setText(slObject.optInt("checkqty") + "");
                                            slItemReportView.getTextView6().setText(slObject.optInt("inqty") + "");
                                            mSlItemViewList.add(slItemReportView);
                                            mLayoutSl.addView(slItemReportView);
                                        }

                                        // 色相数量初始化
                                        JSONArray sxArray = data.optJSONArray("sx");
                                        for (int j = 0; j < sxArray.length(); j++) {
                                            JSONObject sxObject = sxArray.optJSONObject(j);
                                            Report sxReport = new Report();
                                            sxReport.setLot(sxObject.optString("lot"));
                                            sxReport.setLength(sxObject.optString("length_width"));
                                            sxReport.setProperty(sxObject.optString("sx"));
                                            sxReport.setArch(sxObject.optString("jno"));
                                            sxReport.setCheckqty(sxObject.optInt("checkqty"));
                                            sxReport.setInqty(sxObject.optInt("inqty"));
                                            mSxReportList.add(sxReport);
                                            ItemReportView sxItemReportView = new ItemReportView(mContext, R.layout.item_report);
                                            sxItemReportView.getTextView2().setText(sxObject.optString("length_width"));
                                            sxItemReportView.getTextView3().setText(sxObject.optString("sx"));
                                            sxItemReportView.getTextView4().setText(sxObject.optString("jno"));
                                            sxItemReportView.getTextView4().setOnClickListener(new View.OnClickListener() {
                                                @Override
                                                public void onClick(View v) {
                                                    AlertDialog alertDialog1 = new AlertDialog.Builder(mContext)
                                                            .setTitle("券号")//标题
                                                            .setMessage(((TextView) v).getText())//内容
                                                            .create();
                                                    alertDialog1.show();

                                                }
                                            });

                                            sxItemReportView.getTextView5().setText(sxObject.optInt("checkqty") + "");
                                            sxItemReportView.getTextView6().setText(sxObject.optInt("inqty") + "");
                                            mSxItemViewList.add(sxItemReportView);
                                            mLayoutSx.addView(sxItemReportView);
                                        }
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    });
        }
    }
}
